home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 4 / Meeting Pearls Vol. IV (1996)(GTI - Schatztruhe)[!].iso / Pearls / midi / misc / Midi2TeX / src / tp_m2tf4.c < prev    next >
C/C++ Source or Header  |  1994-03-30  |  9KB  |  350 lines

  1. /* Output from p2c, the Pascal-to-C translator */
  2. /* From input file "tp_m2tf2.pas" */
  3.  
  4.  
  5. #define TP_M2TF4_G
  6. #include "tp_m2tf4.h"
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. /**************************************************/
  11. Void ReadBlock(FilRec)
  12. FileRecord *FilRec;
  13. {
  14.   /**************************************************/
  15.   Char STR1[256];
  16.   Char STR2[256];
  17.   size_t  reads;
  18.   char     *p;
  19.   int     ToBeRead,i;
  20.  
  21.   /* there is a request to read a block. If the EOF is reached exit */
  22.   if (FilRec->LastBlockRead)
  23.     ErrorExit(24L);
  24.   fseek(MidiFile, FilRec->FilePosition, SEEK_SET);
  25.   if (FilRec->BufSemaphore > 0) {
  26.     memmove(FilRec->ReadBuf,FilRec->BufPoint,(long)FilRec->BufSemaphore);
  27.     FilRec->BufPoint= FilRec->ReadBuf;
  28.     p=FilRec->ReadBuf;
  29.     for (i=1;i<=FilRec->BufSemaphore;i++){p++;}
  30.   } else {
  31.     FilRec->BufPoint = FilRec->ReadBuf;
  32.     p=FilRec->ReadBuf;
  33.     }
  34.   ToBeRead=sizeof(BufType)-FilRec->BufSemaphore;
  35.   FilRec->ReadIn = fread(p, 1,ToBeRead ,MidiFile);
  36.   FilRec->BufSemaphore += FilRec->ReadIn;
  37.   if (Debug) {
  38.     sprintf(STR2, "Read in: %ld", FilRec->ReadIn);
  39.     WriteDebugInfo(STR2);
  40.     sprintf(STR2, "Bufsemaphore : %d", FilRec->BufSemaphore);
  41.     WriteDebugInfo(STR2);
  42.   }
  43.   if (FilRec->ReadIn < ToBeRead ) {
  44.     FilRec->LastBlockRead = true;
  45.     if (Debug)
  46.       WriteDebugInfo("Read in last block in file");
  47.   } else
  48.     FilRec->LastBlockRead = false;
  49.   FilRec->FilePosition = ftell(MidiFile) /* + 1 */;
  50. }  /* ReadBlock */
  51.  
  52.  
  53. /*************************************************/
  54. uchar ReadByte(FilRec)
  55. FileRecord *FilRec;
  56. {
  57.   /*************************************************/
  58.   uchar Result;
  59.  
  60.   if (FilRec->BufSemaphore < 1)
  61.     ReadBlock(FilRec);
  62.   memmove(&Result, FilRec->BufPoint,1L);
  63.   FilRec->BufSemaphore--;
  64.   FilRec->BufPoint++;
  65.   FilRec->BytesProcessed++;
  66.   FilRec->LastNoBytesRead = 1;
  67.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  68.     FilRec->NoMoreData = false;
  69.     return Result;
  70.   }
  71.   FilRec->NoMoreData = true;
  72.   if (Debug)
  73.     WriteDebugInfo("There are no more data");
  74.   return Result;
  75. }  /* ReadByte */
  76.  
  77.  
  78. #ifndef ST
  79. /*******************************************************/
  80. long ReadLongInt(FilRec)
  81. FileRecord *FilRec;
  82. {
  83.   /*******************************************************/
  84.   long TmpLI, cnt;
  85.   uchar b;
  86.  
  87.   if (FilRec->BufSemaphore < 4)
  88.     ReadBlock(FilRec);
  89.   TmpLI = 0;
  90.   for (cnt = -1; cnt <= 2; cnt++) {
  91.     memmove(&b, FilRec->BufPoint,1L);
  92.     FilRec->BufPoint++;
  93.     TmpLI = TmpLI * 256 + b;
  94.   }
  95. /* p2c: tp_m2tf2.pas, line 92: Note: Can't interpret size in MOVE [174] */
  96.   FilRec->BufSemaphore -= 4;
  97.   FilRec->BytesProcessed += 4;
  98.   FilRec->LastNoBytesRead = 4;
  99.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  100.     FilRec->NoMoreData = false;
  101.     return TmpLI;
  102.   }
  103.   FilRec->NoMoreData = true;
  104.   if (Debug)
  105.     WriteDebugInfo("There are no more data");
  106.   return TmpLI;
  107. }  /* ReadLongInt */
  108.  
  109.  
  110. /*******************************************************/
  111. long ReadInteger(FilRec)
  112. FileRecord *FilRec;
  113. {
  114.   /*******************************************************/
  115.   long TmpInt, cnt;
  116.   uchar b;
  117.   int    i;
  118.  
  119.   if (FilRec->BufSemaphore < 2)
  120.     ReadBlock(FilRec);
  121.   TmpInt = 0;
  122.   for (cnt = -1; cnt <= 0; cnt++) {
  123.     memmove((&b), (FilRec->BufPoint),1L);
  124.     FilRec->BufPoint++;
  125.     TmpInt = TmpInt * 256 + b;
  126.   }
  127.   FilRec->BufSemaphore -= 2;
  128.   FilRec->BytesProcessed += 2;
  129.   FilRec->LastNoBytesRead = 2;
  130.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  131.     FilRec->NoMoreData = false;
  132.     return TmpInt;
  133.   }
  134.   FilRec->NoMoreData = true;
  135.   if (Debug)
  136.     WriteDebugInfo("There are no more data");
  137.   return TmpInt;
  138. }  /* ReadInteger */
  139. #endif
  140.  
  141.  
  142. #ifdef ST
  143. /*******************************************************/
  144. long ReadInteger(FilRec)
  145. FileRecord *FilRec;
  146. {
  147.   /*******************************************************/
  148.   long TmpInt;
  149.  
  150.   if (FilRec->BufSemaphore < 2)
  151.     ReadBlock(FilRec);
  152.   memmove((&TmpInt), (&FilRec->ReadBuf[FilRec->BufPoint - 1]),
  153.       sizeof(uchar));
  154.   FilRec->BufSemaphore -= 2;
  155.   FilRec->BufPoint += 2;
  156.   FilRec->BytesProcessed += 2;
  157.   FilRec->LastNoBytesRead = 2;
  158.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  159.     FilRec->NoMoreData = false;
  160.     return TmpInt;
  161.   }
  162.   FilRec->NoMoreData = true;
  163.   if (Debug)
  164.     WriteDebugInfo("There are no more data");
  165.   return TmpInt;
  166. }  /* ReadInteger */
  167.  
  168.  
  169. /*******************************************************/
  170. long ReadLongInt(FilRec)
  171. FileRecord *FilRec;
  172. {
  173.   /*******************************************************/
  174.   long TmpLI;
  175.  
  176.   if (FilRec->BufSemaphore < 4)
  177.     ReadBlock(FilRec);
  178.   memmove((&TmpLI), (&FilRec->ReadBuf[FilRec->BufPoint - 1]),
  179.       sizeof(uchar) * 2L);
  180.   FilRec->BufPoint += 4;
  181.   FilRec->BufSemaphore -= 4;
  182.   FilRec->BytesProcessed += 4;
  183.   FilRec->LastNoBytesRead = 4;
  184.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  185.     FilRec->NoMoreData = false;
  186.     return TmpLI;
  187.   }
  188.   FilRec->NoMoreData = true;
  189.   if (Debug)
  190.     WriteDebugInfo("There are no more data");
  191.   return TmpLI;
  192. }  /* ReadLongInt */
  193. #endif
  194.  
  195. /******************************************************/
  196. long ReadVarLen(FilRec)
  197. FileRecord *FilRec;
  198. {
  199.   /******************************************************/
  200.   long Result, Tmp;
  201.   uchar Bt, Cnt;
  202.  
  203.   Cnt = 1;
  204.   Tmp = 0;
  205.   Bt = ReadByte(FilRec);
  206.  
  207.   if ((Bt & 0x80) > 0) {
  208.     do {
  209.       Bt &= 0x7f;
  210.       Tmp += Bt;
  211.       Tmp <<= 7;
  212.       Bt = ReadByte(FilRec);
  213.       Cnt++;
  214.     } while ((Bt & 0x80) != 0);
  215.     Tmp += Bt;
  216.   } else
  217.     Tmp = Bt;
  218.  
  219.   Result = Tmp;
  220.   FilRec->LastNoBytesRead = Cnt;
  221.   return Result;
  222. }  /* ReadVarLen */
  223.  
  224.  
  225. /*******************************************************************/
  226. Char *ReadString(Result, FilRec, len)
  227. Char *Result;
  228. FileRecord *FilRec;
  229. long len;
  230. {
  231.   /*******************************************************************/
  232.   Char TmpStr[81];
  233.   int    i;
  234.   if (FilRec->BufSemaphore < len)
  235.     ReadBlock(FilRec);
  236.   TmpStr[len] = '\0';
  237.   memmove(&TmpStr, (FilRec->BufPoint),
  238.       len);
  239.   FilRec->BufSemaphore -= len;
  240.   for (i=0;i<len;i++) FilRec->BufPoint++;
  241.   FilRec->BytesProcessed += len;
  242.   FilRec->LastNoBytesRead = len;
  243.   if (!(FilRec->LastBlockRead==true && FilRec->BufSemaphore==0)) {
  244.     FilRec->NoMoreData = false;
  245.     strcpy(Result, TmpStr);
  246.     return Result;
  247.   }
  248.   FilRec->NoMoreData = true;
  249.   if (Debug)
  250.     WriteDebugInfo("There are no more data");
  251.   strcpy(Result, TmpStr);
  252.   return Result;
  253. }  /* ReadString */
  254.  
  255.  
  256. /*******************************************
  257. Void C2Pstring(Cstr)
  258. Char *Cstr;
  259. {
  260.   /*****************************************
  261.   uchar nilpos;
  262.  
  263.   Cstr['P'] = '\0';
  264.   nilpos = strpos2(Cstr, "\0", 1);
  265.   Cstr[nilpos] = '\0';
  266. /* p2c: tp_m2tf2.pas, line 258:
  267.  * Note: Modification of string length may translate incorrectly [146]
  268. }  /* C2Pstring */
  269.  
  270.  
  271. /********************************************/
  272. Void P2Cstring(Pstr)
  273. Char *Pstr;
  274. {
  275.   /********************************************/
  276.   uchar L;
  277.   int TEMP;
  278.  
  279.   L = strlen(Pstr);
  280.   TEMP = strlen(Pstr);
  281.   memmove(&TEMP, Pstr, (long)L);
  282.   Pstr[L - 1] = '\0';
  283. }  /* C2Pstring */
  284.  
  285.  
  286. /*****************************************************/
  287.   long GetFilePos(FilRec)
  288. /*****************************************************/
  289. FileRecord *FilRec;
  290. {
  291.   /* return ((ftell(MidiFile) + 1) / 500 + FilRec->BufPoint - 1); */
  292.   return (ftell(MidiFile) /* + 1 */) ;
  293. }
  294.  
  295.  
  296. /***********************************************************
  297. Void SetFilePos(FilRec, Pst)
  298. FileRecord *FilRec;
  299. long Pst;
  300. {
  301.   /***********************************************************
  302.   fseek(MidiFile, Pst - 1, SEEK_SET);
  303.   FilRec->BufPoint = BufSize_;
  304.   FilRec->BufSemaphore = 0;
  305.   ReadBlock(FilRec);
  306. } */
  307.  
  308.  
  309.  
  310. /************************************************************/
  311. Void InitFilRec(FilRec)
  312. FileRecord *FilRec;
  313. {
  314.   /************************************************************/
  315.   FilRec->ReadBuf = (char *)malloc(sizeof(BufType));
  316.   if (FilRec->ReadBuf==NULL) ErrorExit(9L);
  317.   memset(FilRec->ReadBuf,'\0', sizeof(BufType));
  318.   FilRec->BufPoint = FilRec->ReadBuf;
  319. }
  320.  
  321.  
  322. /************************************************************/
  323. Void KillFilRec(FilRec)
  324. FileRecord *FilRec;
  325. {
  326.   /************************************************************/
  327. /* p2c: tp_m2tf2.pas, line 314:
  328.  * Warning: Too many arguments for freemem [299] */
  329.   free(FilRec->ReadBuf);
  330. }
  331.  
  332.  
  333. /************************************************************/
  334. Void RestoreLastRead(FilRec)
  335. FileRecord *FilRec;
  336. {
  337.   /************************************************************/
  338.   FilRec->BufPoint -= FilRec->LastNoBytesRead;
  339.   FilRec->BytesProcessed -= FilRec->LastNoBytesRead;
  340.   FilRec->BufSemaphore += FilRec->LastNoBytesRead;
  341. }
  342.  
  343.  
  344. void _TP_M2TF4_init()
  345. {
  346.   static int _was_initialized = 0;
  347.   if (_was_initialized++)
  348.     return;
  349. }
  350.